home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / e_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  3.8 KB  |  177 lines

  1. /*
  2.  * Routines to manipulate the pcomm.extrnl file
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "extrnl.h"
  7.  
  8. /*
  9.  * Read the external file transfer program database.  Returns a pointer
  10.  * to a static area containing the EXTRNL structure.  This support file is 
  11.  * optional.
  12.  */
  13.  
  14. struct EXTRNL *
  15. read_extrnl(extra)
  16. char *extra;
  17. {
  18.     extern char *null_ptr;
  19.     FILE *fp, *my_fopen();
  20.     int i, line, up, entry, oops;
  21.     char *str_dup(), buf[200], message[80], token[40], *str_tok(), *str;
  22.     char *sep, *temp_token, *findfile();
  23.     static struct EXTRNL e;
  24.     void error_win();
  25.  
  26.     if ((e.e_path = findfile(extra, "pcomm.extrnl")) == NULL) {
  27.                     /* not required to exist */
  28.         for (i=0; i<3; i++) {
  29.             e.name[0][i] = null_ptr;
  30.             e.command[0][i] = null_ptr;
  31.             e.prompt[0][i] = 'N';
  32.             e.name[1][i] = null_ptr;
  33.             e.command[1][i] = null_ptr;
  34.             e.prompt[1][i] = 'N';
  35.         }
  36.         e.up_entries = 0;
  37.         e.dn_entries = 0;
  38.  
  39.         return(&e);
  40.     }
  41.  
  42.     if (!(fp = my_fopen(e.e_path, "r"))) {
  43.         sprintf(buf, "\"%s\" for read", e.e_path);
  44.         error_win(1, "Can't open external program file", buf);
  45.     }
  46.  
  47.     sep = ";;\n";
  48.     line = 0;
  49.     up = 1;
  50.     oops = 0;
  51.     while (fgets(buf, 200, fp) != NULL) {
  52.         line++;
  53.         if (line <= 3)
  54.             entry = line-1;
  55.         else {
  56.             up = 0;
  57.             entry = line-4;
  58.         }
  59.                     /* get the token */
  60.         if (!(temp_token = str_tok(buf, '='))) {
  61.             sprintf(message, "is missing a token at line %d", line);
  62.             oops++;
  63.             break;
  64.         }
  65.         /*
  66.          * Parse the rest of the line.  This is similar to using
  67.          * the "real" strtok() function, but this version returns
  68.          * a pointer to NULL if the token is missing.  Note the
  69.          * use of the array of separators.
  70.          */
  71.         for (i=0; i<3; i++) {
  72.             if (!(str = str_tok((char *) NULL, sep[i]))) {
  73.                 sprintf(message, "is missing a parameter at line %d", line);
  74.                 oops++;
  75.                 break;
  76.             }
  77.             switch(i) {
  78.                 case 0:
  79.                     e.name[up][entry] = str_dup(str);
  80.                     break;
  81.                 case 1:
  82.                     e.command[up][entry] = str_dup(str);
  83.                     break;
  84.                 case 2:
  85.                     e.prompt[up][entry] = *str;
  86.                     break;
  87.             }
  88.         }
  89.         if (oops)
  90.             break;
  91.  
  92.                     /* sanity checking */
  93.         if (up)
  94.             sprintf(token, "SEND_%d", entry+1);
  95.         else
  96.             sprintf(token, "RCV_%d", entry+1);
  97.  
  98.         if (strcmp(temp_token, token)) {
  99.             sprintf(message, "is corrupted at line %d", line);
  100.             oops++;
  101.             break;
  102.         }
  103.     }
  104.     fclose(fp);
  105.  
  106.     if (oops) {
  107.         sprintf(buf, "External program file \"%s\"", e.e_path);
  108.         error_win(1, buf, message);
  109.     }
  110.                     /* find number of upload entries */
  111.     for (i=0; i<3; i++) {
  112.         if (e.name[1][i] == null_ptr)
  113.             break;
  114.     }
  115.     e.up_entries = i;
  116.                     /* find number of download entries */
  117.     for (i=0; i<3; i++) {
  118.         if (e.name[0][i] == null_ptr)
  119.             break;
  120.     }
  121.     e.dn_entries = i;
  122.                     /* if empty database */
  123.     if (!e.up_entries || !e.dn_entries) {
  124.         sprintf(buf, "External program file \"%s\"", e.e_path);
  125.         error_win(0, buf, "has no data");
  126.     }
  127.  
  128.     return(&e);
  129. }
  130.  
  131. /*
  132.  * Update the external file transfer program database.  A non-zero return
  133.  * code means a non-fatal error.
  134.  */
  135.  
  136. int
  137. up_extrnl()
  138. {
  139.     FILE *fp, *my_fopen();
  140.     int i, up, entry;
  141.     char buf[200];
  142.     void error_win();
  143.  
  144.     /* 
  145.      * I don't remember why I made this file optional.  For the next
  146.      * release, it will be mandatory!  The following is kludge to tell
  147.      * the user that there is no file to save anything to.
  148.      */
  149.     if (extrnl->e_path == NULL) {
  150.         error_win(0, "No \"pcomm.extrnl\" file in use", "");
  151.         return(1);
  152.     }
  153.                     /* open for write */
  154.     if (!(fp = my_fopen(extrnl->e_path, "w"))) {
  155.         sprintf(buf, "\"%s\"", extrnl->e_path);
  156.         error_win(0, "No write permission on external program file", buf);
  157.         return(1);
  158.     }
  159.                     /* put 'em back */
  160.     up = 1;
  161.     for (i=0; i<6; i++) {
  162.         if (i < 3)
  163.             entry = i;
  164.         else {
  165.             up = 0;
  166.             entry = i-3;
  167.         }
  168.         if (up)
  169.             fprintf(fp, "SEND_%d=%s;%s;%c\n", entry+1, extrnl->name[up][entry], extrnl->command[up][entry], extrnl->prompt[up][entry]);
  170.         else
  171.             fprintf(fp, "RCV_%d=%s;%s;%c\n", entry+1, extrnl->name[up][entry], extrnl->command[up][entry], extrnl->prompt[up][entry]);
  172.     }
  173.  
  174.     fclose(fp);
  175.     return(0);
  176. }
  177.